<?php
/*
 * Copyright notice
 * 
 * (c) 2009 MH-Dev.de, Martin Hesse <info@mh-dev.de>
 * 
 * This script is part of the MH-Dev.-Board project. The MH-Dev.-Board 
 * is free software; you can redistribute it and/or modify it under the 
 * terms of the GNU General Public License as published by 
 * the Free Software Foundation; either version 3 of the License, 
 * or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied warranty 
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU 
 * General Public License along with this program; if not, 
 * see http://www.gnu.org/licenses/.
 */

class MHDEV_View_Helper_GetWatchedThreads extends Zend_View_Helper_Abstract
{
	/*
	 * Gets a list of the last posts in the forum
	 * 
	 * @param int $number_of_results
	 * 
	 * @return Zend_View
	 */
	public function GetWatchedThreads($number_of_results)
	{
		// db
		$notice	= new ThreadsNotice();
		
		// query
		$select = $notice->select()->setIntegrityCheck(false);
		$select->from(
			array('w' => 'threads_notice')
		)
		->join(
			array('t' => 'threads'), 'w.thread_uid = t.uid', array('title AS thread_title', 'board_uid', 'last_reply_date', 'last_reply_user_uid')
		)
		->join(
			array('p' => 'posts'), 't.uid = p.thread_uid', array('COUNT("p.uid") AS new_posts')
		)
		->joinLeft(
			array('u' => 'user'), 't.last_reply_user_uid = u.uid', array('username')
		)
		->join(
			array('b' => 'boards'), 't.board_uid = b.uid', array('title AS board_title')
		)
		->join(
			array('c' => 'categories'), 'b.category_uid = c.uid', array('title AS category_title') 
		)
		->order('t.create_date DESC')
		->where('w.user_uid = ?', Zend_Auth::getInstance()->getIdentity()->uid)
		->where('t.is_public = ?', 1)
		->where('t.is_deleted = ?', 0)
		->where('p.create_date > w.last_post')
		->group('t.uid')
		->limit(intval($number_of_results));

		$query 		= $notice->fetchAll($select);
		
		$results	= $query->toArray();
		
		if(isset($results[0]['thread_title']))
		{
			$this->view->GLP_count	= $query->count();
			$this->view->GLP_result	= $query->toArray();
		}
		else
		{
			$this->view->GLP_count = 0;
		}
		
		return $this->view->render('helper/GetWatchedThreads.phtml');
	}
}
?>